Load Libraries

library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ✔ purrr   0.3.5      
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(here)
## here() starts at C:/Users/cindy/OneDrive/Desktop/Repositories/Fajardo
library(ggsn)
## Loading required package: grid
library(emojifont)
register_google(key = "AIzaSyDy4dZ675IcOI8hIqCsoWeV8E4QYVSfduI")

Load Data

ChemData<-read_csv(here("Week_07","Data","chemicaldata_maunalua.csv"))
## Rows: 23 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): Zone, Site, Season, Tide_time
## dbl (11): Waypoint, Lat, Long, Temp_in, Salinity, Phosphate, Silicate, NN, p...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glimpse(ChemData)
## Rows: 23
## Columns: 15
## $ Waypoint    <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ Zone        <chr> "Transition", "Transition", "Transition", "Transition", "D…
## $ Lat         <dbl> 21.27531, 21.27523, 21.27504, 21.27449, 21.27503, 21.27485…
## $ Long        <dbl> -157.7618, -157.7627, -157.7633, -157.7640, -157.7617, -15…
## $ Site        <chr> "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W"…
## $ Season      <chr> "SPRING", "SPRING", "SPRING", "SPRING", "SPRING", "SPRING"…
## $ Tide_time   <chr> "Low_Day", "Low_Day", "Low_Day", "Low_Day", "Low_Day", "Lo…
## $ Temp_in     <dbl> 23.75506, 23.53256, 22.63450, 24.01982, 23.26102, 24.00517…
## $ Salinity    <dbl> 27.74029, 30.61192, 28.37008, 32.82124, 29.12293, 34.02018…
## $ Phosphate   <dbl> 0.54, 0.36, 0.50, 0.25, 0.50, 0.13, 0.28, 0.15, 0.23, 0.11…
## $ Silicate    <dbl> 157.93, 92.59, 143.60, 42.32, 126.47, 15.04, 56.31, 23.10,…
## $ NN          <dbl> 7.92, 3.37, 7.29, 0.79, 7.45, 0.46, 1.59, 0.34, 1.91, 0.25…
## $ pH          <dbl> 7.909, 7.965, 8.023, 7.995, 8.005, 8.019, 8.003, 7.978, 7.…
## $ TA          <dbl> 2161.482, 2145.828, 2272.391, 2219.583, 2151.826, 2216.758…
## $ percent_sgd <dbl> 20.4043928, 11.9625323, 18.5529716, 5.4677003, 16.3397933,…

Get base maps from ggmap

get_map() is the function to get a basemap from Google Maps. At it’s simplest form, you only need to put in a location. Use ggmap to plot the base layer

Oahu<-get_map("Oahu")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=Oahu&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Oahu&key=xxx
ggmap(Oahu)

# Get coordinates for Wailupe on Oahu

You can also put in lat and lon coordinates to get a basemap.

#Make a data frame of lon and lat coordinates
WP<-data.frame(lon = -157.7621, lat = 21.27427) # coordinates for Wailupe
# Get base layer
Map1<-get_map(WP)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
# plot it
ggmap(Map1)

# Zoom in on a location

The zoom argument, an integer from 3 to 20 specifying how large the spatial extent should be around the center, with 3 being the continent level and 20 being roughly the single building level.

Map1<-get_map(WP,zoom = 17)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
ggmap(Map1)

# Change the map type

Map1<-get_map(WP,zoom = 17, maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxx
ggmap(Map1)

Map1<-get_map(WP,zoom = 17, maptype = "watercolor")
## maptype = "watercolor" is only available with source = "stamen".
## resetting to source = "stamen"...
## Source : https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx
## Source : http://tile.stamen.com/watercolor/17/8095/57604.jpg
## Source : http://tile.stamen.com/watercolor/17/8096/57604.jpg
## Source : http://tile.stamen.com/watercolor/17/8097/57604.jpg
## Source : http://tile.stamen.com/watercolor/17/8095/57605.jpg
## Source : http://tile.stamen.com/watercolor/17/8096/57605.jpg
## Source : http://tile.stamen.com/watercolor/17/8097/57605.jpg
## Source : http://tile.stamen.com/watercolor/17/8095/57606.jpg
## Source : http://tile.stamen.com/watercolor/17/8096/57606.jpg
## Source : http://tile.stamen.com/watercolor/17/8097/57606.jpg
## Source : http://tile.stamen.com/watercolor/17/8095/57607.jpg
## Source : http://tile.stamen.com/watercolor/17/8096/57607.jpg
## Source : http://tile.stamen.com/watercolor/17/8097/57607.jpg
ggmap(Map1)

# You can use the ggmap base layer in any ggplot

Map1<-get_map(WP,zoom = 17, maptype = "satellite") 
## Source : https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxx
ggmap(Map1)+
  geom_point(data = ChemData,
             aes(x = Long, y = Lat, color = Salinity),
             size = 4) +
  scale_color_viridis_c()

# Add scale bar

ggmap(Map1)+
  geom_point(data = ChemData, 
             aes(x = Long, y = Lat, color = Salinity), 
             size = 4) + 
  scale_color_viridis_c()+
  scalebar( x.min = -157.766, x.max = -157.758, #tells where to put bar on x axis
           y.min = 21.2715, y.max = 21.2785, #tells where to put bar n y axis
            dist = 250, #show distance so 0-250
           dist_unit = "m", #units we want to show distance
           model = "WGS84", #specific to gps model you are working with
            transform = TRUE, st.color = "pink",
            box.fill = c("yellow", "white"))

# Don’t know the exact lat and long?

Use geocode() to get exact locations that you can then use in the maps.

geocode("the white house") #gets long and lat of white house
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=the+white+house&key=xxx
## # A tibble: 1 × 2
##     lon   lat
##   <dbl> <dbl>
## 1 -77.0  38.9
geocode("California State University, Northridge") #gets lat and long of center of csun
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=California+State+University,+Northridge&key=xxx
## # A tibble: 1 × 2
##     lon   lat
##   <dbl> <dbl>
## 1 -119.  34.2